package com.zslibrary.StateHandle;

import android.os.SystemClock;

import com.zslibrary.Enum.StateDefine;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;

public class WorkingState extends StateHandle {
    private WorkInvoke mWorker;
    private Pipe mPipe;
    private Boolean mWorkingResult=false;

    public WorkingState(Pipe pipe){
        mPipe=pipe;
    }

    @Override
    public @StateEventDefine.StateEvent int handle(){
        mWorkingResult=false;
        int event= mWorker.worker(mPipe);
        mWorkingResult=true;
        return event;
    }

    @Override
    public @StateDefine.State int getNextHop(@StateEventDefine.StateEvent int event) {
        if(event==StateEventDefine.EVT_RESET)
            return StateDefine.STATE_INIT;
        else if(event==StateEventDefine.EVT_EXECUTE)
            return StateDefine.STATE_BUSY;
        else if(event==StateEventDefine.EVT_DISCONNECT)
            return StateDefine.STATE_ERROR;
        return StateDefine.STATE_IDLE;
    }

    public interface WorkInvoke{
        @StateEventDefine.StateEvent int worker(Pipe pipe);
    }

    public void setWorkInvoke(WorkInvoke work){
        mWorker=work;
    }

    public void waitWorkingResult(){
        while(!mWorkingResult){
            SystemClock.sleep(10);
        }
    }

}
